//	Copyright (C) 2010  Thomas Andersen (thomas.1.andersen@gmail.com)
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//	
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//	
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.	


function CreateSafe_callBack(doc)
{
    var parser = new XMLRequestParser(doc);
    if (parser.CheckError())
    {
        alert(parser.GetError());
        document.SafeNewForm.Submit.disabled = false;
    }
    else
    {
        var html = "<div id=info>";
        html += "OK";
        html += "</div>";
        document.getElementById("maininfo").innerHTML = html;
        SafeNew('','','');
    }
}


function SafeCreate() {
    document.SafeNewForm.Submit.disabled = true;
    var pl = new XMLReqParameters();
    var sess = readCookie('OpenSecretSession');
    pl.add("sessionid", sess);
	pl.add("key", readCookie('OpenSecretKey'));
	var b = new Blowfish("Name" + readCookie('OpenSecretKey'));
    pl.add("name", b.encrypt("'" + document.SafeNewForm.name.value + "'"));
	var b = new Blowfish("User" + readCookie('OpenSecretKey'));
    pl.add("user", b.encrypt("'" + document.SafeNewForm.user.value + "'"));
	var b = new Blowfish("Pass" + readCookie('OpenSecretKey'));
    pl.add("password", b.encrypt("'" + document.SafeNewForm.pass.value + "'"));
    XMLReq.invoke("service.php", "CreateSafe", pl, false, CreateSafe_callBack);
}

function SafeDelete() {
    document.SafeNewForm.Submit.disabled = true;
    var pl = new XMLReqParameters();
    var sess = readCookie('OpenSecretSession');
    pl.add("sessionid", sess);
	pl.add("key", readCookie('OpenSecretKey'));
	var b = new Blowfish("Name" + readCookie('OpenSecretKey'));
    pl.add("name", b.encrypt("'" + document.SafeNewForm.name.value + "'"));
    XMLReq.invoke("service.php", "DeleteSafe", pl, false, CreateSafe_callBack);
}

function SafeNew(name, user, pass) {
	name = encode(unescape(name));
	user = encode(unescape(user));
	pass = encode(unescape(pass));

    var html = "<form class=safenewform id=\"SafeNewForm\" name=\"SafeNewForm\" onsubmit=\"return false;\">";
    html += "<fieldset>";
    html += "<legend>Safe Item</legend>";
    html += "<label for=name>Name:</label><input class=formitem type=\"text\" id=\"name\" name=\"name\" autocomplete=\"off\" maxlength=100 value=\"" + name + "\"/><br/>";
    html += "<label for=name>User:</label><input class=formitem type=\"text\" id=\"user\" name=\"user\" autocomplete=\"off\" maxlength=200 value=\"" + user + "\"/><br/>";
    html += "<label for=name>Password:</label><input class=formitem type=\"text\" id=\"pass\" name=\"pass\" autocomplete=\"off\" maxlength=200 value=\"" + pass + "\"/><br/>";
    html += "<input class=formitembutton type=submit name=Submit value=Create onClick=\"SafeCreate();\">";
    html += "<input class=formitembutton type=submit name=Submit value=Delete onClick=\"SafeDelete();\">";
    html += "</fieldset>";
    html += "</form>";
    document.getElementById("mainform").innerHTML = html;
	document.SafeNewForm.name.focus();

}


function SafeSearch() {
    document.SafeForm.Submit.disabled = true;
    var pl = new XMLReqParameters();
    var sess = readCookie('OpenSecretSession');
    pl.add("sessionid", sess);
	pl.add("key", readCookie('OpenSecretKey'));
    pl.add("name", document.SafeForm.name.value);
	createCookie('OpenSecretSearch', document.SafeForm.name.value,1);
    XMLReq.invoke("service.php", "SafeSearch", pl, false, SafeSearch_callBack);
}

function ShowSafe()
{
    var html = "<form class=safeform id=\"SafeForm\" name=\"SafeForm\" onsubmit=\"SafeSearch(); return false;\">";
    html += "<fieldset>";
    html += "<legend>Search</legend>";
    html += "<label for=name>Name:</label><input class=formitem type=\"text\" id=\"name\" name=\"name\" size=\"20\" maxlength=45/><br/>";
    html += "<input class=formitembutton type=submit name=Submit value=Search>";
    html += "</fieldset>";
    html += "</form>";
    document.getElementById("mainform").innerHTML = html;
	document.getElementById("maininfo").innerHTML = "";
	document.SafeForm.name.focus();

}


function SafeSearch_callBack(doc, o)
{
    var parser = new XMLRequestParser(doc);
    if (parser.CheckError())
    {
        alert(parser.GetError());
        document.SafeForm.Submit.disabled = false;
    }
    else
    {
        document.SafeForm.Submit.disabled = false;
        var i = 0;
        var html = "<TABLE class=SafeTable id=SafeTable border='1' frame='border' rules='groups' width='100%'>";
        html = html + "<colgroup></colgroup><colgroup></colgroup><colgroup></colgroup>";
        html = html + "<thead><tr><th width=50%>Name</th><th width=15%>User</th><th width=20%>Password</th>";
        html = html + "</tr></thead><tbody>";
        var name = parser.GetField("name" + i);
        var user = parser.GetField("user" + i);
        var pass = parser.GetField("password" + i);
		
        while(name != null)
        {
			if (!user)
				user = "";
			if (!pass)
				pass = "";
			var b = new Blowfish("Name" + readCookie('OpenSecretKey'));
			name = b.decrypt(name);
			name = name.substr(1,name.lastIndexOf("'")-1);
			pos = name.toLowerCase().indexOf( readCookie('OpenSecretSearch').toLowerCase() );
			if (pos != -1) {			
				var b = new Blowfish("User" + readCookie('OpenSecretKey'));
				user = b.decrypt(user);
				user = user.substr(1,user.lastIndexOf("'")-1);
				var b = new Blowfish("Pass" + readCookie('OpenSecretKey'));
				pass = b.decrypt(pass);
				pass = pass.substr(1,pass.lastIndexOf("'")-1);
				html = html + "<TR onClick=\"SafeNew('" + escape(name) + "','" + escape(user) + "','" + escape(pass) + "');\">";
				name = encode(name);
				user = encode(user);
				pass = encode(pass);
				html = html + "<TD class=newuseritem>" + name + "</TD>";
				html = html + "<TD class=newuseritemcount>" + user + "</TD>";
				html = html + "<TD class=newuseritemprice>" + pass + "</TD>";
				html = html + "</TR>";
			}
            i++;
            name = parser.GetField("name" + i);
            user = parser.GetField("user" + i);
            pass = parser.GetField("password" + i);
        }
        html += "</tbody></TABLE>";
        document.getElementById("maininfo").innerHTML = html;
		eraseCookie('OpenSecretSearch');
    }
} 

